/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.namabank.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;

/**
 *
 * @author Hienforis
 */
public class DateTimeUtil {

    public DateTimeUtil() {
    }

    public static String getVietStringDate(Date date) {
        SimpleDateFormat dateformat = new SimpleDateFormat(VIETDATEFORMAT);
        return dateformat.format(date);
    }

    public static String getNgayThangNamString(Date date, boolean upperfirstchar) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        String res = "";
        if (upperfirstchar) {
            res += "Ngày ";
        } else {
            res += "ngày ";
        }
        res += cal.get(Calendar.DAY_OF_MONTH);
        res += " tháng " + (cal.get(Calendar.MONTH) + 1);
        res += " năm " + cal.get(Calendar.YEAR);
        return res;
    }
    public static String getVietStringDateTime(Date date) {
        SimpleDateFormat dateformat = new SimpleDateFormat(VIETDATETIMEFORMAT);
        return dateformat.format(date);
    }
    public static Date fromVietStringDate(String sdate) throws Exception {
        if (sdate == null || sdate.length() == 0) {
            throw new Exception("sdate is null or empty");
        }
        SimpleDateFormat dateformat = new SimpleDateFormat(VIETDATEFORMAT);
        return dateformat.parse(sdate.trim());
    }
    public static Date fromVietStringDateOrNull(String sdate){
        if (sdate == null || sdate.length() == 0) {
            return null;
        }
        try{
            SimpleDateFormat dateformat = new SimpleDateFormat(VIETDATEFORMAT);
            return dateformat.parse(sdate.trim());
        }catch(Exception ex){
            return null;
        }
    }
    public static Date fromVietStringMonth(String sdate) throws ParseException {
        SimpleDateFormat dateformat = new SimpleDateFormat(VIETMONTHFORMAT);
        return dateformat.parse(sdate);
    }
    public static String toVietStringMonth(Date date) {
        SimpleDateFormat dateformat = new SimpleDateFormat(VIETMONTHFORMAT);
        return dateformat.format(date);
    }
    public static Date switchToEndDay(Date date) {
        Calendar cal = Calendar.getInstance();

        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    public static Date switchTimeToNow(Date date){
        Calendar cal = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date);
        cal2.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        cal2.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
        cal2.set(Calendar.SECOND, cal.get(Calendar.SECOND));
        cal2.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
        return cal2.getTime();
    }

    public static Calendar switchToEndMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        if (cal.get(Calendar.MONTH) == 1 && cal.get(Calendar.YEAR) % 4 == 0) {
            cal.set(Calendar.DAY_OF_MONTH, 29);
        } else {
            cal.set(Calendar.DAY_OF_MONTH, DAYS_IN_MONTHS[cal.get(Calendar.MONTH)]);
        }
        return cal;
    }

    public static Date switchToStartDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }
    public static Date switchToStartMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    public static int compareDate(Date date1, String date2) throws Exception {
        Date date = fromVietStringDate(date2);
        return compareDate(date1, date);
    }

    public static int compareDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
            return -1;
        }
        if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) {
            return 1;
        }
        if (cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR)) {
            return -1;
        }
        if (cal1.get(Calendar.DAY_OF_YEAR) > cal2.get(Calendar.DAY_OF_YEAR)) {
            return 1;
        }
        return 0;
    }

    public static int compareMonth(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
            return -1;
        }
        if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) {
            return 1;
        }
        if (cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH)) {
            return -1;
        }
        if (cal1.get(Calendar.MONTH) > cal2.get(Calendar.MONTH)) {
            return 1;
        }
        return 0;
    }

    public static int getQuarter(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getQuarter(cal);
    }
    public static int getQuarter(Calendar cal) {
        int month = cal.get(Calendar.MONTH);

        return (month >= Calendar.JANUARY && month <= Calendar.MARCH) ? 1
                : (month >= Calendar.APRIL && month <= Calendar.JUNE) ? 2
                : (month >= Calendar.JULY && month <= Calendar.SEPTEMBER) ? 3
                : 4;
    }
    public static int getYear(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }
    public static int getMonth(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }
    public static final String VIETDATEFORMAT = "dd/MM/yyyy";
    public static final String VIETMONTHFORMAT = "MM/yyyy";
    public static final String VIETDATETIMEFORMAT = "dd/MM/yyyy HH:mm:ss";
    public static final int[] DAYS_IN_MONTHS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}
